package cn.copera.dyy.web;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.copera.dyy.common.web.BaseController;
import cn.copera.dyy.entity.PointPraiseSet;
import cn.copera.dyy.entity.Shop;
import cn.copera.dyy.service.PointPraiseSetService;
import cn.copera.dyy.service.ShopService;

/**
 * 点赞Controller
 * 
 * 
 */
@Controller
@RequestMapping(value = "${adminPath}/pointpraise")
public class PointPraiseSetController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	PointPraiseSetService praiseSetService;
	@Autowired
	private ShopService shopService;

	// 开放
	@RequestMapping(value = "/enabled", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> enabled(
			@RequestParam(required = false) String shopId, String type) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"点赞");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("点赞");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				if (type.equals("1")) {
					pointPraiseSet.setEnabled("开放");
				} else {
					pointPraiseSet.setEnabled("关闭");
				}

				praiseSetService.save(pointPraiseSet);

			} else {
				if (type.equals("1")) {
					pointPraiseSet.setEnabled("开放");
				} else {
					pointPraiseSet.setEnabled("关闭");
				}
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("pointPraiseSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

	// 累计点赞数
	@RequestMapping(value = "/total", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> total(
			@RequestParam(required = false) String shopId, String totalCount) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"点赞");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("点赞");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setTotalCount(Integer.parseInt(totalCount
						.replace("次", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setTotalCount(Integer.parseInt(totalCount
						.replace("次", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("pointPraiseSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

	// 赠送点卡
	@RequestMapping(value = "/presenter", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> presenter(
			@RequestParam(required = false) String shopId, String totalCount) {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"点赞");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				pointPraiseSet.setType("点赞");
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setPresenterCount(Integer.parseInt(totalCount
						.replace("个", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setPresenterCount(Integer.parseInt(totalCount
						.replace("个", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("pointPraiseSet", pointPraiseSet);
		} catch (Exception e) {
			map.put("message", "error");
		}
		return map;

	}

	//每人每天最多点赞
	@RequestMapping(value = "/day", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> day(
			@RequestParam(required = false) String shopId, String totalCount) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PointPraiseSet pointPraiseSet = praiseSetService
					.findPointPraiseSeByShop(shopId,"点赞");
			if (pointPraiseSet == null) {
				pointPraiseSet = new PointPraiseSet();
				Shop shop = shopService.get(shopId);
				pointPraiseSet.setType("点赞");
				pointPraiseSet.setShop(shop);
				pointPraiseSet.setDayCount(Integer.parseInt(totalCount
						.replace("次", "")));

				praiseSetService.save(pointPraiseSet);

			} else {
				pointPraiseSet.setDayCount(Integer.parseInt(totalCount
						.replace("次", "")));
				praiseSetService.save(pointPraiseSet);
			}
			map.put("message", "success");
			map.put("pointPraiseSet", pointPraiseSet);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("message", "error");
		}
		return map;

	}

}
